package com.lznAdmin.base.common.api.controller;

import com.lznAdmin.base.common.api.service.CommonAPI;
import com.lznAdmin.base.common.vo.DictVO;
import com.lznAdmin.base.lznAdminResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashMap;
import java.util.List;

/**
 * 系统公共API Controller
 *
 * @author lzn
 * @create: 2022-02-25 11:24
 **/
@Slf4j
@Api(tags = "数据字典控制器")
@RestController
@RequestMapping("/api")
public class CommonAPIController {

    @Autowired
    CommonAPI commonAPI;

    @ApiOperation(value = "获取字典明细数据", httpMethod = "GET", notes = "")
    @GetMapping("/getDictByCode")
    public lznAdminResult getDictByCode(@RequestParam("dictCode") String dictCode) {
        List<DictVO> dictVOS = commonAPI.translateDict(dictCode);
        if (dictVOS == null) {
            return lznAdminResult.no();
        }
        return lznAdminResult.ok(dictVOS);
    }

    @ApiOperation(value = "获取表字典数据", httpMethod = "GET", notes = "")
    @GetMapping("/getTableDictByCode")
    public lznAdminResult getTableDictByCode(@RequestParam("dictCode") String dictCode, @RequestParam("isCache") Integer isCache) {
        List<LinkedHashMap<String, Object>> translateTableDict = commonAPI.translateTableDict(dictCode, isCache);
        if (translateTableDict == null) {
            return lznAdminResult.no();
        }
        return lznAdminResult.ok(translateTableDict);
    }

    @ApiOperation(value = "主动清除字典缓存数据", httpMethod = "POST", notes = "如果dictCode为null或空，则全部清除")
    @PostMapping("/clearDictCache")
    public lznAdminResult clearDictCache(String dictCode) {
        commonAPI.ClearDictCache(dictCode);
        return lznAdminResult.ok("已清除！");
    }
}
